package com.copc.mq.config;

import com.copc.mq.bean.SysConstant;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.amqp.core.Message;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * RabbitMQ配置类
 * @author yuanbin
 * @version 1.0
 */

@Configuration
public class RabbitMQConfig {

    @Autowired
    private MessageConverter messageConverter;

    @Autowired
    @Qualifier("prototypeRabbitConnectionFactory")
    private ObjectProvider<com.rabbitmq.client.ConnectionFactory> rabbitConnectionFactoryProvider;

    @Autowired
    @Qualifier("prototypeAmqpConnectionFactory")
    private ObjectProvider<ConnectionFactory> amqpConnectionFactoryProvider;

    @Scope(SCOPE_PROTOTYPE)
    @Bean(name="prototypeRabbitConnectionFactory", destroyMethod = "destroy")
    public RabbitConnectionFactoryBean rabbitConnectionFactory(
            String uri
    ) {
        RabbitConnectionFactoryBean bean = new RabbitConnectionFactoryBean();
        bean.setUri(uri);
        return bean;
    }

    @Scope(SCOPE_PROTOTYPE)
    @Bean(name="prototypeAmqpConnectionFactory", destroyMethod = "destroy")
    public ConnectionFactory amqpConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) {
        return new CachingConnectionFactory(rabbitConnectionFactory);
    }

    @Scope(SCOPE_PROTOTYPE)
    @Bean(name="prototypeAmqpTemplate", destroyMethod = "destroy")
    public AmqpTemplate amqpTemplate(
            String uri
    ) throws Exception {
        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = rabbitConnectionFactoryProvider.getObject(uri);
        ConnectionFactory amqpConnectionFactory = amqpConnectionFactoryProvider.getObject(rabbitConnectionFactory);
        RabbitTemplate template = new RabbitTemplate(amqpConnectionFactory);
        template.setMessageConverter(messageConverter);
        return template;
    }


    /**
     * COPC_ISV处理订单支付回调消息队列 queue
     * @return
     */
    @Bean
    public Queue payedQueue(){
        return new Queue(SysConstant.COPC_ISV_PAYED_QUEUE1);
    }


    /**
     * COPC_ISV处理订单取消回调消息
     * @return
     */
    @Bean
    public Queue cancelQueue() {
        return new Queue(SysConstant.COPC_ISV_CANCEL_QUEUE1);
    }

    /**
     * COPC_ISV券核销订单回调消息
     * @return
     */
    @Bean
    public Queue voucherQueue() {
        return new Queue(SysConstant.COPC_ISV_VOUCHER_QUEUE1);
    }

    /**
     * COPC_ISV分发到商城订单消息
     * @return
     */
    @Bean
    public Queue disorderQueue() {
        return new Queue(SysConstant.COPC_ISV_DISORDER_QUEUE1);
    }


}
